package com.qs.eggs.view.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class GonggaoActivity extends Activity {
    private String id;
    private  String note;
    private  WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gonggao);
        id=getIntent().getStringExtra("id");
        webView=(WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDefaultTextEncodingName("utf-8") ;
        getDetails();
    }
    private void getDetails(){

        Map<String, String> map1 = new HashMap<>();
        map1.put("id",id);
        Gson gson = new Gson();
        String str = gson.toJson(map1);
        OkGo.post(Constants.URL_BASE+"notice?action=getNoticeDetail")
                .params("params",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("公告详情--" + response);
                        try {
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                JSONObject js=jsonArray.getJSONObject(i);
                                note=js.getString("note");
                                webView.setWebViewClient(new MyWebViewClient());
                                webView.setWebChromeClient(new WebChromeClient());
                                webView.loadDataWithBaseURL("http://118.190.47.231/",note,"text/html","utf-8",null);
                                //webView.loadData(note, "text/html; charset=UTF-8", null);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                });
    }
    class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            // html加载完成之后，添加监听图片的点击js函数
            //addImageClickListner();
            imgReset();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    /**
     * 循环遍历标签中的图片
     * js 语法
     */
    private void imgReset() {
        webView.loadUrl("javascript:(function(){" +
                "var objs = document.getElementsByTagName('img'); " +
                "for(var i=0;i<objs.length;i++)  " +
                "{"
                + "var img = objs[i];   " +
                "    img.style.maxWidth = '100%';   " +
                "}" +
                "})()");
    }
}
